Skip to content

Conversation

ghost
Copy link

@ghost ghost commented Oct 10, 2013

Added capabilities for models to get and set nested attributes using dot notation or camel-case.

$user = User::find($id);

$firstName = $user->nameFirst;

The above code would pull attributes from

$model->attributes['name']['first'];
$user->nameFirst = "David";

This would set the the attribute

$model->attribute['name']['first']
public function setNameFirstAttribute($value)
{
    $this->attributes['name']['first'] = $value;
}

public function getNameFirstAttribute($value)
{
    return "First name : " . $value;
}

public function getNameAttribute($value)
{
    $first = $this->getAttribute("name.first");
    $last = $this->getAttribute("name.last");

    return $first . ' ' . $last;
}

Mutators work using dot notation with the getAttribute() and setAttribute()

@ghost
Copy link
Author

ghost commented Oct 10, 2013

Having spent some more time thinking this through, I don't think camel case is the way to go as that would conflict with the Eloquent way of getting underscored attributes. Maybe underscores to denote array depth with camel case attributes. Again this is a touchy subject as there would need to be database naming convention compromises made. Some suggestion on this would be good.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants